package com.smit.livevideo.sample.dialog;

import android.app.Activity;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Created by smfang on 2015/12/28.
 * DialogFragment基类
 */
public class BaseDialogFragment extends DialogFragment implements OnClickListener
{
    protected Activity activity = null;
    protected TextView contentTextView = null;

    @Override
    public void onAttach(Activity activity)
    {
        super.onAttach(activity);
        this.activity = activity;
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        DialogFragmentUtil.addDialogFragment(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        return null;
    }

    /**
     * 将idOne和idTwo两字符串拼接，中间加个换行符,即两行显示.
     * @param idOne 字串id1
     * @param idTwo 字串id2
     */
    public void setDialogContent(int idOne, int idTwo)
    {
        if (contentTextView != null)
        {
            StringBuilder sb = new StringBuilder();
            String stringIdOne = activity.getResources().getString(idOne);
            String stringIdTwo = activity.getResources().getString(idTwo);
            sb.append(stringIdOne).append('\n').append(stringIdTwo);
            contentTextView.setText(sb.toString());
        }
    }

    /**
     * 显示内容不需要拼接.
     * @param id 需要显示的字串id
     */
    public void setDialogContent(int id)
    {
        if (contentTextView != null)
        {
            contentTextView.setText(getResources().getString(id));
        }
    }

    @Override
    public void onCancel(DialogInterface dialog)
    {
        super.onCancel(dialog);
    }

    @Override
    public void onDismiss(DialogInterface dialog)
    {
        if (DialogFragmentUtil.peekDialogFragment() == this)
        {
            DialogFragmentUtil.popDialogFragment();
        }
        super.onDismiss(dialog);
    }

//    @Override
//    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
//    {
//        if (keyCode == KeyEvent.KEYCODE_BACK)
//        {
//            dismiss();
//            return true;
//        }
//
//        return false;
//    }

    /**
     * 继承的子类去实现按键响应的操作,这里不做操作
     * @param v 响应按键的视图
     */
    @Override
    public void onClick(View v)
    {
    }

}
